package com.smartwebx.ha.biz.assign.impl;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.smartwebx.ha.biz.assign.HaAreaManager;
import com.smartwebx.ha.biz.assign.HaDirectoryManager;
import com.smartwebx.ha.biz.assign.HaReportAreaManager;
import com.smartwebx.ha.biz.assign.HaReportManager;
import com.smartwebx.ha.biz.assign.HaReportUserManager;
import com.smartwebx.ha.biz.assign.HaTMymeunManager;
import com.smartwebx.ha.biz.assign.HaUserManager;
import com.smartwebx.ha.dal.assign.bean.HaArea;
import com.smartwebx.ha.dal.assign.bean.HaAreaExample;
import com.smartwebx.ha.dal.assign.bean.HaDirectory;
import com.smartwebx.ha.dal.assign.bean.HaPage;
import com.smartwebx.ha.dal.assign.bean.HaReportArea;
import com.smartwebx.ha.dal.assign.bean.HaReportUser;
import com.smartwebx.ha.dal.assign.bean.HaTMymeun;
import com.smartwebx.ha.dal.assign.bean.HaUser;
import com.smartwebx.ha.dal.assign.dao.HaAreaMapper;
import com.smartwebx.ha.dal.assign.dao.HaPageMapper;
import com.smartwebx.ha.usebean.UserTreeNode;
import com.smartwebx.ha.util.BasicTreeNode;
import com.smartwebx.ha.util.HaToolBiz;
/**
 * 
 * 创建人：Dream~天涯 
 * 创建时间：2015-4-20 下午4:01:45
 * 邮箱：154494796@qq.com
 * 备注：
 */
@Service
public class HaReportManagerImpl implements HaReportManager {
	@Autowired
	private HaPageMapper haPageMapper;
	@Autowired
	private HaAreaMapper haAreaMapper;
	@Autowired
	private HaDirectoryManager haDirectoryManager;
	@Autowired
	private HaReportUserManager haReportUserManager;
	@Autowired
	private HaReportAreaManager haReportAreaManager;
	@Autowired
	private HaTMymeunManager haTMymeunManager;
	@Autowired
	private HaUserManager haUserManager;
	@Autowired
	private HaAreaManager haAreaManager;
	@Override
	public List<UserTreeNode> getTree(Integer userId, Integer roleId, Integer areaId, String code, String treeUserId, Integer reportType) {
		Boolean checked = false;
		String iconFolderopen = "icon-folderopen";
		String iconNew = "icon-new";
		String icon = null;
		StringBuffer haDirectoryIdBuffer = new StringBuffer();
		if(treeUserId!=null&&!"".equals(treeUserId)){
			List<HaReportUser> haReportUserLists = haReportUserManager.getReportUserData("userId", Integer.parseInt(treeUserId.substring(1, treeUserId.length())));
			for(HaReportUser haReportUserList : haReportUserLists){
				List<HaDirectory> haDirectory = haDirectoryManager.getDirectoryData("reportId", haReportUserList.getPageId(),reportType);
				if(haDirectory.size()==0){
					continue;
				}
				haDirectoryIdBuffer.append("\""+haDirectory.get(0).getId());	
			}
		}
		haDirectoryIdBuffer.append("\"");	
		
		List<UserTreeNode> userTreeNodes = new ArrayList<UserTreeNode>();
		StringBuffer reportIdBuffer = new StringBuffer();
		StringBuffer areaIdBuffer = new StringBuffer();
		if(roleId==1||roleId==2){
			int directoryId;
			int pageId;
			String pageIdString;
			
			List<HaDirectory> haDirectorys = haDirectoryManager.getDirectoryFromCodeAndIsLeaf(code, 0);
			if(haDirectorys.get(0).getIsLeaf()==0){
				icon = iconFolderopen;
			}else if(haDirectorys.get(0).getIsLeaf()==1){
				icon = iconNew;
				reportIdBuffer.append("\""+haDirectorys.get(0).getReportId());
			}
			
			checked = HaToolBiz.getChecked(haDirectoryIdBuffer.toString(), haDirectorys.get(0).getId().toString());
			UserTreeNode userTreeNode = new UserTreeNode("D"+haDirectorys.get(0).getId(), haDirectorys.get(0).getId(), "D"+haDirectorys.get(0).getParentId().toString(), haDirectorys.get(0).getName(), icon, checked);
			userTreeNodes.add(userTreeNode);
			directoryId = haDirectorys.get(0).getId();
			
			List<HaDirectory> haDirectorys2 = haDirectoryManager.getDirectoryData("parentId", directoryId, reportType);
			for(HaDirectory haDirectory2 : haDirectorys2){
				if(haDirectory2.getIsLeaf()==0){
					icon = iconFolderopen;
				}else if(haDirectory2.getIsLeaf()==1){
					icon = iconNew;
					reportIdBuffer.append("\""+haDirectory2.getReportId());
				}
				
				checked = HaToolBiz.getChecked(haDirectoryIdBuffer.toString(), haDirectory2.getId().toString());
				UserTreeNode userTreeNode2 = new UserTreeNode("D"+haDirectory2.getId(), haDirectory2.getId(), "D"+haDirectory2.getParentId().toString(), haDirectory2.getName(), icon, checked);
				userTreeNodes.add(userTreeNode2);
				
				List<HaDirectory> haDirectorys3 = haDirectoryManager.getDirectoryData("parentId", haDirectory2.getId(), reportType);
				for(HaDirectory haDirectory3 : haDirectorys3){
					if(haDirectory3.getIsLeaf()==0){
						icon = iconFolderopen;
					}else if(haDirectory3.getIsLeaf()==1){
						icon = iconNew;
						reportIdBuffer.append("\""+haDirectory3.getReportId());
					}
					checked = HaToolBiz.getChecked(haDirectoryIdBuffer.toString(), haDirectory3.getId().toString());
					UserTreeNode userTreeNode3 = new UserTreeNode("D"+haDirectory3.getId(), haDirectory3.getId(), "D"+haDirectory3.getParentId().toString(), haDirectory3.getName(), icon, checked);
					userTreeNodes.add(userTreeNode3);
					
					List<HaDirectory> haDirectorys4 = haDirectoryManager.getDirectoryData("parentId", haDirectory3.getId(), reportType);
					for(HaDirectory haDirectory4 : haDirectorys4){
						if(haDirectory4.getIsLeaf()==0){
							icon = iconFolderopen;
						}else if(haDirectory4.getIsLeaf()==1){
							icon = iconNew;
							reportIdBuffer.append("\""+haDirectory4.getReportId());
						}
						checked = HaToolBiz.getChecked(haDirectoryIdBuffer.toString(), haDirectory4.getId().toString());
						UserTreeNode userTreeNode4 = new UserTreeNode("D"+haDirectory4.getId(), haDirectory4.getId(), "D"+haDirectory4.getParentId().toString(), haDirectory4.getName(), icon, checked);
						userTreeNodes.add(userTreeNode4);
						
						List<HaDirectory> haDirectorys5 = haDirectoryManager.getDirectoryData("parentId", haDirectory4.getId(), reportType);
						for(HaDirectory haDirectory5 : haDirectorys5){
							if(haDirectory5.getIsLeaf()==0){
								icon = iconFolderopen;
							}else if(haDirectory5.getIsLeaf()==1){
								icon = iconNew;
								reportIdBuffer.append("\""+haDirectory5.getReportId());
							}
							checked = HaToolBiz.getChecked(haDirectoryIdBuffer.toString(), haDirectory5.getId().toString());
							UserTreeNode userTreeNode5 = new UserTreeNode("D"+haDirectory5.getId(), haDirectory5.getId(), "D"+haDirectory5.getParentId().toString(), haDirectory5.getName(), icon, checked);
							userTreeNodes.add(userTreeNode5);
							
							List<HaDirectory> haDirectorys6 = haDirectoryManager.getDirectoryData("parentId", haDirectory5.getId(), reportType);
							for(HaDirectory haDirectory6 : haDirectorys6){
								if(haDirectory6.getIsLeaf()==0){
									icon = iconFolderopen;
								}else if(haDirectory6.getIsLeaf()==1){
									icon = iconNew;
									reportIdBuffer.append("\""+haDirectory6.getReportId());
								}
								checked = HaToolBiz.getChecked(haDirectoryIdBuffer.toString(), haDirectory6.getId().toString());
								UserTreeNode userTreeNode6 = new UserTreeNode("D"+haDirectory6.getId(), haDirectory6.getId(), "D"+haDirectory6.getParentId().toString(), haDirectory6.getName(), icon, checked);
								userTreeNodes.add(userTreeNode6);
							}
						}
					}
				}
			}
			reportIdBuffer.append("\"");
			List<HaReportArea> haReportAreas = haReportAreaManager.getReportAreaData("areaId", areaId);
			for(HaReportArea haReportArea : haReportAreas){
				pageId = haReportArea.getPageId();
				pageIdString = "\"" + pageId + "\"";
				if((reportIdBuffer.toString()).indexOf(pageIdString)==-1){
					reportIdBuffer.append("\""+pageId+"\"");
					List<HaDirectory> haDirectorys4 = haDirectoryManager.getDirectoryData("reportId", haReportArea.getPageId(), reportType);
					if(haDirectorys4.size()==0){
						continue;
					}
					checked = HaToolBiz.getChecked(haDirectoryIdBuffer.toString(), haDirectorys4.get(0).getId().toString());
					UserTreeNode userTreeNode4 = new UserTreeNode("A"+haReportArea.getPageId(),haReportArea.getPageId(), "D"+haDirectorys4.get(0).getParentId().toString(), haDirectorys4.get(0).getName(), iconNew, checked);
					userTreeNodes.add(userTreeNode4);
					try {
						areaIdBuffer.append("\"");
						userTreeNodes = addUserTreeNode(areaIdBuffer, haReportArea.getAreaId(),userTreeNodes,haDirectorys4.get(0).getParentId());
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
			}
		}else if(roleId==3){
			List<HaReportUser> haReportUsers = haReportUserManager.getReportUserData("userId", userId);
			for(HaReportUser haReportUser : haReportUsers ){
				List<HaDirectory> haDirectorys = haDirectoryManager.getDirectoryData("reportId", haReportUser.getPageId(), reportType);
				if(haDirectorys.size()==0){
					continue;
				}
				checked = HaToolBiz.getChecked(haDirectoryIdBuffer.toString(), haDirectorys.get(0).getId().toString());
				UserTreeNode userTreeNode = new UserTreeNode("U"+haDirectorys.get(0).getId(),haDirectorys.get(0).getId(), "D"+haDirectorys.get(0).getParentId().toString(), haDirectorys.get(0).getName(), iconNew, checked);
				userTreeNodes.add(userTreeNode);
				try {
					areaIdBuffer.append("\"");
					userTreeNodes = addUserTreeNode(areaIdBuffer, haDirectorys.get(0).getParentId(),userTreeNodes, haDirectorys.get(0).getParentId());
				} catch (IOException e) {
					e.printStackTrace();
				}
			
			}
		}
		return userTreeNodes;
	}
	
	public List<UserTreeNode> addUserTreeNode(StringBuffer areaIdBuffer, Integer areaId, List<UserTreeNode> userTreeNodes, int parentId) throws IOException{
		if((areaIdBuffer.toString()).indexOf("\""+areaId+"\"")==-1){
			areaIdBuffer.append(areaId+"\"");
			HaDirectory haDirectorys6 = haDirectoryManager.select(parentId);
			/*HaDirectoryExample haDirectoryExample6 = new HaDirectoryExample();
			Criteria haDirectorycriteria6 = haDirectoryExample6.createCriteria();
			haDirectorycriteria6.andIdEqualTo(parentId);
			List<HaDirectory> haDirectorys6 = haDirectoryMapper.selectByExample(haDirectoryExample6);*/
			UserTreeNode userTreeNode6 = new UserTreeNode("D"+haDirectorys6.getId(), haDirectorys6.getId(), "D"+haDirectorys6.getParentId().toString(), haDirectorys6.getName(), "icon-folderopen", false);
			userTreeNodes.add(userTreeNode6);
			if(haDirectorys6.getParentId()!=0){
				addUserTreeNode(areaIdBuffer, haDirectorys6.getParentId(), userTreeNodes,haDirectorys6.getParentId());
			}
		}
		return userTreeNodes;
	}
	
	@Override
	public int insert(HaPage haPage) {
		return this.haPageMapper.insertSelective(haPage);
	}
	@Override
	public int delete(int reportId) {
		return this.haPageMapper.deleteByPrimaryKey(reportId);
	}
	@Override
	public List<UserTreeNode> getUserTree(Integer userId, Integer userTreeType, String driectorId) {
		Boolean checked = false;
		String iconFolderopen = "icon-folderopen";
		String iconNew = "icon-new";
		String icon = null;
		StringBuffer haReportAreaAreaIdBuffer = new StringBuffer();
		if(driectorId!=null&&!"".equals(driectorId)){
			HaDirectory haDirectory = haDirectoryManager.select(Integer.parseInt(driectorId));
			List<HaReportArea> haReportAreas = haReportAreaManager.getReportAreaData("pageId", haDirectory.getReportId());
			for(HaReportArea haReportArea : haReportAreas){
				haReportAreaAreaIdBuffer.append("\""+haReportArea.getAreaId());	
			}
		}
		haReportAreaAreaIdBuffer.append("\"");	
		
		HaUser haUser = haUserManager.findById(userId);
		int areaId = haUser.getAreaId();
		HaArea haArea = haAreaManager.findOne(areaId);
		int parentId = haArea.getParentId();
		List<UserTreeNode> userTreeNodes = new ArrayList<UserTreeNode>();
		if(parentId==0){
			userTreeNodes.add(new UserTreeNode("A0", 0, null, "目录", iconFolderopen, false));
			HaAreaExample haAreaExample = new HaAreaExample();
			List<HaArea> haAreaLists = this.haAreaMapper.selectByExample(haAreaExample);
			for(HaArea haAreaList : haAreaLists){
				checked = HaToolBiz.getChecked(haReportAreaAreaIdBuffer.toString(), haAreaList.getId().toString());
				UserTreeNode userTreeNode = new UserTreeNode("A"+haAreaList.getId().toString(), haAreaList.getId(), "A"+haAreaList.getParentId().toString(), haAreaList.getName(), iconFolderopen, checked);
				userTreeNodes.add(userTreeNode);
				if(userTreeType!=1){
					List<HaUser> haUserLists = haUserManager.getUserData("areaId", haAreaList.getId().toString());
					for(HaUser haUserList : haUserLists){
						checked = HaToolBiz.getChecked(haReportAreaAreaIdBuffer.toString(), haUserList.getAreaId().toString());
						UserTreeNode userTreeNode2 = new UserTreeNode("U"+haUserList.getId().toString(),haUserList.getId(), "A"+haUserList.getAreaId().toString(), haUserList.getName(), iconNew, checked);
						userTreeNodes.add(userTreeNode2);
					}
				}
			}
		}else if(parentId==1){
			userTreeNodes.add(new UserTreeNode("A1", 0, null, "目录", null, false));
			HaArea haAreaList = haAreaManager.findOne(areaId);
				checked = HaToolBiz.getChecked(haReportAreaAreaIdBuffer.toString(), haAreaList.getId().toString());
				UserTreeNode userTreeNode = new UserTreeNode("A"+haAreaList.getId().toString(), haAreaList.getId(), "A"+haAreaList.getParentId().toString(), haAreaList.getName(), iconFolderopen, checked);
				userTreeNodes.add(userTreeNode);
				List<HaUser> haUserLists = haUserManager.getUserData("areaId", haAreaList.getId().toString());
				for(HaUser haUserList : haUserLists){
					checked = HaToolBiz.getChecked(haReportAreaAreaIdBuffer.toString(), haUserList.getAreaId().toString());
					UserTreeNode userTreeNode2 = new UserTreeNode("U"+haUserList.getId().toString(),haUserList.getId(), "A"+haUserList.getAreaId().toString(), haUserList.getName(), iconNew, checked);
					userTreeNodes.add(userTreeNode2);
				}
				
				List<HaArea> haAreaLists = haAreaManager.getAreaData("areaId", haAreaList.getId().toString());
				for(HaArea haAreaList2 : haAreaLists){
					checked = HaToolBiz.getChecked(haReportAreaAreaIdBuffer.toString(), haAreaList2.getId().toString());
					UserTreeNode userTreeNode2 = new UserTreeNode("A"+haAreaList2.getId().toString(), haAreaList2.getId(), "A"+haAreaList2.getParentId().toString(), haAreaList2.getName(), iconFolderopen, checked);
					userTreeNodes.add(userTreeNode2);
				}
		}else{
			
		}
		return userTreeNodes;
	}

	@Override
	public List<BasicTreeNode> getCommonTree(Integer userId, Integer roleId,
			Integer areaId, String code, String treeUserId) {
		List<BasicTreeNode> basicTreeNodes = new ArrayList<BasicTreeNode>();
		List<HaDirectory> haDirectorys = haDirectoryManager.getDirectoryData("reportType", 2,2);
		for(HaDirectory haDirectory : haDirectorys){
			BasicTreeNode basicTreeNode = new BasicTreeNode(haDirectory.getId(), haDirectory.getParentId(),haDirectory.getName(), "");
			basicTreeNodes.add(basicTreeNode);
		}
		
		return basicTreeNodes;
	}

	@Override
	public List<UserTreeNode> getUserReportTree(Integer userId, Integer roleId,
			Integer areaId, String code, String treeUserId) {
		
		
		
		Boolean checked = false;
		String iconNew = "icon-new";
		StringBuffer haDirectoryIdBuffer = new StringBuffer();
		if(treeUserId!=null&&!"".equals(treeUserId)){
			List<HaReportUser> haReportUserLists = haReportUserManager.getReportUserData("userId", Integer.parseInt(treeUserId.substring(1, treeUserId.length())));
			for(HaReportUser haReportUserList : haReportUserLists){
				List<HaDirectory> haDirectory = haDirectoryManager.getDirectoryData("reportId", haReportUserList.getPageId());
				if(haDirectory.size()==0){
					continue;
				}
				haDirectoryIdBuffer.append("\""+haDirectory.get(0).getId());	
			}
		}
		haDirectoryIdBuffer.append("\"");	
		
		List<UserTreeNode> userTreeNodes = new ArrayList<UserTreeNode>();
		List<HaDirectory> haDirectorys2 = haDirectoryManager.getDirectoryData("reportType",10);
		for(HaDirectory haDirectory2 : haDirectorys2 ){
			UserTreeNode userTreeNode2 = new UserTreeNode("D"+haDirectory2.getId(), haDirectory2.getId(), "D"+haDirectory2.getParentId().toString(), haDirectory2.getName(), "icon-folderopen", haDirectory2.getReportType());
			userTreeNodes.add(userTreeNode2);
		}
		
		
		List<HaReportArea> haReportAreas = haReportAreaManager.getReportAreaData("areaId", areaId);
		for(HaReportArea haReportArea : haReportAreas ){
			List<HaDirectory> haDirectorys = haDirectoryManager.getDirectoryData("reportId", haReportArea.getPageId());
			if(haDirectorys.size()==0){
				continue;
			}
			checked = HaToolBiz.getChecked(haDirectoryIdBuffer.toString(), haDirectorys.get(0).getId().toString());
			UserTreeNode userTreeNode = new UserTreeNode("U"+haDirectorys.get(0).getId(),haDirectorys.get(0).getId(), "D"+haDirectorys.get(0).getParentId().toString(), haDirectorys.get(0).getName(), iconNew, haDirectorys.get(0).getReportType());
			userTreeNodes.add(userTreeNode);
		}
		
		return userTreeNodes;
	}
	
}
